package com.hoki.zj.hrm.controller;

import com.hoki.zj.hrm.dto.AliPayNotifyDto;
import com.hoki.zj.hrm.service.IPayService;
import com.hoki.zj.hrm.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayService payService;

    /**
     * 1.发起支付申请
     */
    @PostMapping("/ali/{orderNo}")
    public AjaxResult payApply(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.me().setResultObj(payService.payApply(orderNo));
    }

    /**
     * 支付宝同步回调
     * @param out_trade_no  交易单号
     */
    @RequestMapping("/return")
    public AjaxResult handlerReturn(String out_trade_no){
        return AjaxResult.me().setResultObj("支付成功");
    }

    /**
     * 支付宝异步回调
     * @param dto AliPayNotifyDto
     * @return
     */
    @RequestMapping("/notify")
    public String handlerNotify(AliPayNotifyDto dto){
        try {
            return payService.notifyHandler(dto);
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
    }
}
